package com.webcams.ui.compass;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.webcams.core.location.ResortLocationManager;
import com.webcams.ui.R;

public class CompassUI extends Activity {

	private CompassView compassView;
	private TextView accuracyView;
	private TextView errorNoGps;
	private Button buttonPlus;
	private Button buttonMinus;
	private RelativeLayout layout;
	private CameraPreview cameraPreview;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);


		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.compass);
		layout = (RelativeLayout) findViewById(R.id.compass_layout);
		buttonPlus = (Button) findViewById(R.id.compass_plus);
		buttonMinus = (Button) findViewById(R.id.compass_minus);
		accuracyView = (TextView) findViewById(R.id.compass_accuracy);
		errorNoGps = (TextView) findViewById(R.id.compass_error_nogps);
		
		int defaultMode = CompassView.MODE_COMPASS;
		
		compassView = new CompassView(this, defaultMode);
		compassView.onLocationChanged(ResortLocationManager.getInstance(this).getLastLocation());

		cameraPreview = new CameraPreview(this, compassView);		

		switch(defaultMode) {
		case CompassView.MODE_COMPASS:
			toCompassMode();
			break;
		case CompassView.MODE_AUGMENTEDREALITY:
			toRealityAugmentedMode();
			break;
		}
		
		buttonPlus.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				if(compassView != null) {
					compassView.resizeRadius(true);
				}
			}
		});


		buttonMinus.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				if(compassView != null) {
					compassView.resizeRadius(false);
				}
			}
		});


	}

	@Override
	protected void onDestroy(){
		super.onDestroy();
		ResortLocationManager.getInstance(this).removeListener(compassView);
		if(compassView != null) {
			compassView.removeListeners();
		}
	}


	public void setNoGps() {
		Log.v("DEBUG","!! no gps");
		if(!errorNoGps.isShown()) {
			errorNoGps.setVisibility(View.VISIBLE);
			accuracyView.setVisibility(View.INVISIBLE);
			if(compassView != null) {
				compassView.setVisibility(View.INVISIBLE);
				buttonPlus.setVisibility(View.INVISIBLE);
				buttonMinus.setVisibility(View.INVISIBLE);
			}
		}
	}

	public void setGPSFine(float f) {
		if(!accuracyView.isShown()) {
			errorNoGps.setVisibility(View.INVISIBLE);
			accuracyView.setVisibility(View.VISIBLE);
			if(f == -1) {
				accuracyView.setText("");
			} else {
				accuracyView.setText(getResources().getString(R.string.compass_accuracy)+ " "+f+"m");
			}
			if(compassView != null) {
				compassView.setVisibility(View.VISIBLE);
				buttonPlus.setVisibility(View.VISIBLE);
				buttonMinus.setVisibility(View.VISIBLE);
			}
		}	
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
	    super.onConfigurationChanged(newConfig);
	}
	
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_compass, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.compass_menu_augmentedreality:
        	toRealityAugmentedMode();
        	return true;
        case R.id.compass_menu_compass:
        	toCompassMode();
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    public void toRealityAugmentedMode() {
		setContentView(cameraPreview);
		layout.removeView(compassView);
		addContentView(compassView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		compassView.setMode(CompassView.MODE_AUGMENTEDREALITY);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    
    public void toCompassMode() {
		setContentView(layout);
		layout.removeView(compassView);
		layout.addView(compassView);
		compassView.setMode(CompassView.MODE_COMPASS);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }
}
